package com.huhao.smark;

import static android.content.Context.MODE_PRIVATE;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;

public class SettingsFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    private Button logoutButton;
    private SharedPreferences sharedPreferences;
    private boolean isLoggedIn = false;
    private LinearLayout accountSecurityLayout;
    private LinearLayout generalSettingsLayout;
    private LinearLayout featuresLayout;
    private LinearLayout helpFeedbackLayout;
    private LinearLayout aboutLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        logoutButton = view.findViewById(R.id.logout_button);
        accountSecurityLayout = view.findViewById(R.id.account_security);
        generalSettingsLayout = view.findViewById(R.id.general_settings);
        featuresLayout = view.findViewById(R.id.features);
        helpFeedbackLayout = view.findViewById(R.id.help_feedback);
        aboutLayout = view.findViewById(R.id.about);

        // 从 SharedPreferences 中读取登录状态
        sharedPreferences = requireActivity().getSharedPreferences("app_prefs", MODE_PRIVATE);
        isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);

        // 注册 SharedPreferences 监听器
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);

        // 根据登录状态设置按钮的可见性
        updateLogoutButtonVisibility();

        // 设置按钮点击事件，添加确认弹窗
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建并显示确认弹窗
                new AlertDialog.Builder(requireContext())
                        .setTitle("确认注销")
                        .setMessage("您确定要注销登录吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedPreferences.Editor editor = sharedPreferences.edit();

                                editor.putBoolean("isLoggedIn", false);
                                editor.remove("token");
                                editor.remove("account");
                                editor.remove("username");
                                editor.remove("bookCount");
                                editor.remove("id");
                                editor.remove("avatar");

                                editor.apply();
                                updateLogoutButtonVisibility();
                                Toast.makeText(requireContext(), "注销成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });

        // 设置其他点击区域的点击事件
        setupClickListeners();

        return view;
    }

    private void setupClickListeners() {
        // 账号安全
        accountSecurityLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLoggedIn) {
                    // 未登录时跳转到登录页面并提示
                    Toast.makeText(requireContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(requireContext(), LoginActivity.class));
                } else {
                    startActivity(new Intent(requireContext(), AccountSecurityActivity.class));
                }
            }
        });
        // 通用设置
        generalSettingsLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireContext(), GeneralSettingsActivity.class));
            }
        });
        // 功能介绍
        featuresLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireContext(), FeaturesActivity.class));
            }
        });
        // 帮助与反馈
        helpFeedbackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireContext(), HelpFeedbackActivity.class));
            }
        });
        // 关于
        aboutLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireContext(), AboutActivity.class));
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (sharedPreferences != null) {
            sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("isLoggedIn")) {
            isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
            updateLogoutButtonVisibility();
        }
    }

    private void updateLogoutButtonVisibility() {
        if (isLoggedIn) {
            logoutButton.setVisibility(View.VISIBLE);
        } else {
            logoutButton.setVisibility(View.GONE);
        }
    }
}